package com.lxw;

import com.rabbitmq.client.*;

import java.util.HashMap;

public class Producer05_header {
    //队列名
    private static final String QUEUE_HEADER_EMAIL = "queue_header_email";
    private static final String QUEUE_HEADER_SMS = "queue_header_sms";
    private static final String EXCHANGE_HEADER_INFORM = "exchange_header_inform";

    public static void main(String[] args)throws Exception {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        connectionFactory.setVirtualHost("/");

        //创建一个连接
        Connection connection = connectionFactory.newConnection();
        //创建与交换机的通道，每个通道代表一个会话
        Channel channel = connection.createChannel();
        /**
         * exchangeDeclare(交换机名称，交换机类型)
         * 交换机的类型有：fanout、topic、direct、headers
         */
//        channel.exchangeDeclare(EXCHANGE_HEADER_INFORM, BuiltinExchangeType.HEADERS);

        HashMap<String, Object> header_sms = new HashMap<>();
        header_sms.put("inform_type","sms");

        HashMap<String, Object> header_email = new HashMap<>();
        header_email.put("inform_type2","email");

        HashMap<String, Object> header_all = new HashMap<>();
        header_all.put("inform_type","sms");
        header_all.put("inform_type2","email");

        AMQP.BasicProperties.Builder properties_email = new AMQP.BasicProperties.Builder().headers(header_email);

        AMQP.BasicProperties.Builder properties_sms = new AMQP.BasicProperties.Builder().headers(header_sms);

        AMQP.BasicProperties.Builder properties_all = new AMQP.BasicProperties.Builder().headers(header_all);

//        channel.queueDeclare(QUEUE_HEADER_SMS,true,false,false,null);
//        channel.queueDeclare(QUEUE_HEADER_EMAIL,true,false,false,null);
//
//
//        channel.queueBind(QUEUE_HEADER_SMS,EXCHANGE_HEADER_INFORM,"all.#");
//        channel.queueBind(QUEUE_HEADER_EMAIL,EXCHANGE_HEADER_INFORM,"all.sms.email.#.aa");

        for (int i=0;i<4;i++){
            String message = "email inform to user" + i;
            channel.basicPublish(EXCHANGE_HEADER_INFORM,"",properties_email.build(),message.getBytes());
            System.out.println(message);
        }

        for (int i=0;i<4;i++){
            String message = "sms inform to user" + i;
            channel.basicPublish(EXCHANGE_HEADER_INFORM,"",properties_sms.build(),message.getBytes());
            System.out.println(message);
        }

        for (int i=0;i<4;i++){
            String message = "sms email inform to user" + i;
            channel.basicPublish(EXCHANGE_HEADER_INFORM,"",properties_all.build(),message.getBytes());
            System.out.println(message);
        }
    }
}
